package com.deer.wms.kuaidiniao.constant;

import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;


/**
 * @author zhengkai.blog.csdn.net
 */
@ServerEndpoint("/printTest")
@Component
public class WebSocketServer {

     /**静态变量，用来记录当前在线连接数。应该把它设计成线程安全的。*/
    private static int onlineCount = 0;

    /**与某个客户端的连接会话，需要通过它来给客户端发送数据*/
    private static Session session;
    /**接收userId*/
    private String userId="";

    public static void sandMassage(String message) throws IOException {
        session.getBasicRemote().sendText(message);
    }

    /**
     * 连接建立成功调用的方法*/
    @OnOpen
    public void onOpen(Session session1) {
        session = session1;


        try {
            sendMessage("连接成功");
        } catch (IOException e) {

        }
    }

    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose() {

    }

    /**
     * 收到客户端消息后调用的方法
     *
     *    客户端发送过来的消息*/
    @OnMessage
    public void onMessage(String test) {


            try {
                 String aaa = test;
                 String bbb = null+aaa;
            }catch (Exception e){
                e.printStackTrace();
            }

    }

    /**
     *
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {
         error.printStackTrace();
    }
    /**
     * 实现服务器主动推送
     */
    public void sendMessage(String message) throws IOException {
        session.getBasicRemote().sendText(message);
    }



    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    public static synchronized void addOnlineCount() {
      }

    public static synchronized void subOnlineCount() {
      }
}

